Vector2 = function(x, y) {
	this.x = x == null ? 0 : x;
	this.y = y == null ? 0 : y;

	if (x != null && y == null) {
		this.x = x;
		this.y = x;
	}

	this.previousX = 0;
	this.previousY = 0;
};

Vector2.prototype.Set = function(x, y) {
	this.previousX = this.x;
	this.previousY = this.y;

	if (x != null && y == null) {
		this.x = x;
		this.y = x;
	}

	this.x = x == null ? this.x : x;
	this.y = y == null ? this.x : y;
};

Vector2.prototype.Normalize = function() {
	var tmp = new Vector2(this.x, this.y);
	var mag = Math.sqrt(tmp.x * tmp.x + tmp.y * tmp.y);

	tmp.x = tmp.x / mag;
	tmp.y = tmp.y / mag;

	return tmp;
};

/**
 *	Retorna un valor entero [1,4] que representa el sector del vector.
 *  */
Vector2.prototype.GetSec = function() {
	var x = this.x;
	var y = this.y;

	if (y >= 0) {
		if (x >= 0)
			return 1;
		if (x <= 0)
			return 2;
	}

	if (y <= 0) {
		if (x <= 0)
			return 3;
		if (x >= 0)
			return 4;
	}
};

/**
 *	Retorna el angulo que describe el vector en grados.
 *  */
Vector2.prototype.Angle = function() {
	var angle = Math.atan(this.y / this.x);
	angle = angle * (180 / Math.PI);

	var sec = this.GetSec() - 1;

	angle = angle + (90 * sec);

	return angle;
};

Vector2.prototype.Distance = function(vec2) {
	var aux1;
	var aux2;

	if (vec2 != null) {
		aux1 = vec2.x - this.x;
		aux2 = this.y - vec2.y;
	} else {
		aux1 = this.previousX - this.x;
		aux2 = this.previousY - this.y;
	}

	return Math.sqrt(aux1 * aux1 + aux2 * aux2);
};

Vector2.prototype.HasChanged = function() {
	if (this.previousX != this.x || this.y != this.previousY)
		return true;
	else
		return false;
};

Vector2.prototype.Difference = function(vec2, invert) {
	var inv = 1;
	if (invert)
		inv = -1;

	if (vec2 == null) {
		return new Vector2((this.x - this.previousX) * inv, (this.y - this.previousY) * inv);
	} else {
		return new Vector2((this.x - vec2.x) * inv, (this.y - vec2.y) * inv);
	}
};

/**
 *	Retorna un vector diferencia entre vec2 y el vector mismo (this).<br/>
 * var xx = vec2.x - this.x; <br/>
 var yy = vec2.y - this.y; <br/>
 return new Vector2(xx, yy);
 *  */
Vector2.prototype.GetDiffVec = function(vec2) {
	var xx = vec2.x - this.x;
	var yy = vec2.y - this.y;
	return new Vector2(xx, yy);
};

Vector2.prototype.Move = function(vec2) {
	this.x += vec2.x;
	this.y += vec2.y;
};

Vector2.prototype.GetDirection = function() {
	var angle = this.Angle();

	if (angle >= 338 || angle < 23)
		return Direction.right;
	if (angle >= 23 && angle < 68)
		return Direction.upRight;
	if (angle >= 68 && angle < 113)
		return Direction.up;
	if (angle >= 113 && angle < 158)
		return Direction.upLeft;
	if (angle >= 158 && angle < 203)
		return Direction.left;
	if (angle >= 203 && angle < 248)
		return Direction.downLeft;
	if (angle >= 248 && angle < 293)
		return Direction.down;
	if (angle >= 293 && angle < 338)
		return Direction.downRight;

};
